package com.zone.vehicle.superApps.model;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.zone.sdk.common.http.base.Result;
import com.zone.sdk.common.http.callback.ResultCallBack;
import com.zone.sdk.module.appstore.base.AppStoreResult;
import com.zone.sdk.module.appstore.entity.AppEntity;
import com.zone.sdk.module.comment.base.CommentPage;
import com.zone.sdk.module.comment.entity.AppCommentScoreEntity;
import com.zone.sdk.module.comment.entity.CommentEntity;
import com.zone.sdk.module.comment.entity.CommentListEntity;
import com.zone.sdk.module.comment.entity.PublisherEntity;
import com.zone.sdk.module.comment.entity.RecommendEntity;
import com.zone.sdk.module.comment.entity.RecommendListEntity;
import com.zone.sdk.module.comment.entity.ReplyEntity;
import com.zone.sdk.module.comment.entity.SearchResultEntity;
import com.zone.vehicle.superApps.repository.HomeRepository;
import com.zone.vehicle.superApps.utils.BeanUtils;

import org.apache.commons.lang3.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper=false)
public class HomeViewModel extends ViewModel {

    private static final String TAG = HomeViewModel.class.getSimpleName();
    //首页APP list
    private MutableLiveData<AppStoreResult<AppEntity>> homeAppList = new MutableLiveData<>();
    //详情页app info
    private MutableLiveData<AppEntity> currentApp = new MutableLiveData<>();
    //详情页评论
    private MutableLiveData<AppCommentScoreEntity> commentScore = new MutableLiveData<>();
    private MutableLiveData<CommentListEntity> currentCommentList = new MutableLiveData<>();
    //安利墙
    private MutableLiveData<List<RecommendEntity>> recommendList = new MutableLiveData<>();
    private MutableLiveData<List<AppEntity>> recommendAppList = new MutableLiveData<>();
    //搜索
    private MutableLiveData<String> searchData = new MutableLiveData<>();
    private MutableLiveData<List<SearchResultEntity>> searchAppList = new MutableLiveData<>();

    private HomeRepository repository;

    public HomeViewModel(HomeRepository repository){
        this.repository = repository;
    }

    public MutableLiveData<AppStoreResult<AppEntity>> getHomeAppList() {
        return homeAppList;
    }
    public MutableLiveData<CommentListEntity> getCurrentCommentList() {
        return currentCommentList;
    }

    public MutableLiveData<List<AppEntity>> getRecommendAppList() {
        return recommendAppList;
    }

    public MutableLiveData<String> getSearchData() {
        return searchData;
    }

    public void setSearchData(String searchData) {
        this.searchData.setValue(searchData);
    }

    public MutableLiveData<List<SearchResultEntity>> getSearchAppList() {
        return searchAppList;
    }

    public MutableLiveData<AppCommentScoreEntity> getCommentScore() {
        return commentScore;
    }

    private static final boolean isDebug = false;

    @SuppressLint("CheckResult")
    public void getHomeAppListData(){

        if (isDebug){
//            homeAppList.setValue(getAppEntityList());
        }else {//ResultCallBack<Result<AppStoreResult<AppEntity>>>
            repository.getHomeAppListData(new ResultCallBack<Result<AppStoreResult<AppEntity>>>() {
                @Override
                public void onSuccess(Result<AppStoreResult<AppEntity>> listAppStoreResult) {
//                Log.e(TAG, "onSuccess:"+listAppStoreResult);
                    homeAppList.setValue(listAppStoreResult.getData());
                }

                @Override
                public void onError(String errorCode, String message) {
                    Log.e(TAG, "onError:"+errorCode);
                }

                @Override
                public void onComplete() {

                }
            });
        }
    }

    public void getAppDetail(){
        AppEntity appEntity = currentApp.getValue();
        if (ObjectUtils.isEmpty(appEntity)) {
            return;
        }
        if (isDebug){
            // TODO test
            AppEntity appEntity1 = new AppEntity();
            BeanUtils.copyProperties(appEntity, appEntity1);
            currentApp.setValue(appEntity1);
//            currentCommentList.setValue(getcurrentCommentList());
            // TODO test end
        }else {
            repository.getAppDetail(appEntity.getCode(), new ResultCallBack<Result<AppEntity>>() {
                @Override
                public void onSuccess(Result<AppEntity> result) {
//                Log.e(TAG, "onSuccess:"+result);
                    if (result.getResultCode().equals("200")) {
                        currentApp.setValue(result.getData());
                        getCommentListData(result.getData());
                        commentCommentScore(result.getData());
                    }
                }

                @Override
                public void onError(String errorCode, String message) {

                }

                @Override
                public void onComplete() {

                }
            });
        }
    }

    public void commentCommentScore(AppEntity appEntity){
        repository.commentScore(appEntity.getCode(), new ResultCallBack<Result<AppCommentScoreEntity>>() {
            @Override
            public void onSuccess(Result<AppCommentScoreEntity> commentScoreEntityResult) {
                commentScore.setValue(commentScoreEntityResult.getData());
            }

            @Override
            public void onError(String errorCode, String message) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    public void getCommentListData(AppEntity appEntity){
        repository.commentList(appEntity.getCode(), new ResultCallBack<Result<CommentListEntity>>() {
            @Override
            public void onSuccess(Result<CommentListEntity> result) {
//                Log.e(TAG, "onSuccess:"+result);
                if (result.getResultCode().equals("200") && ObjectUtils.isNotEmpty(result.getData())) {
                    currentCommentList.setValue(result.getData());
                }
            }

            @Override
            public void onError(String errorCode, String message) {
                Log.e(TAG, "errorCode:"+errorCode);
            }

            @Override
            public void onComplete() {

            }
        });
    }

    public void commentInitAmwayWallList(){
        if (isDebug){
            recommendAppList.setValue(getAppEntityList());
        }else {
            repository.commentInitAmwayWallList(new ResultCallBack<Result<RecommendListEntity>>() {
                @Override
                public void onSuccess(Result<RecommendListEntity> pageResultResult) {
                    Log.e(TAG, "getRecommendListData onSuccess:"+pageResultResult);
                    recommendList.setValue(pageResultResult.getData().getDataList());
                    getRecommendApp(pageResultResult.getData().getDataList());
                }

                @Override
                public void onError(String errorCode, String message) {
                    Log.e(TAG, "getRecommendListData onError:"+errorCode);
                }

                @Override
                public void onComplete() {

                }
            });
        }
    }

    public void downLoadApp(Context context, int status){
        repository.downLoadApp(context, status, getCurrentApp().getValue());
    }

    private void getRecommendApp(List<RecommendEntity> recommendEntityList){
        if (ObjectUtils.isEmpty(recommendEntityList)){
            return;
        }

        List<String> appIds = recommendEntityList.stream().map(RecommendEntity::getObjectId).collect(Collectors.toList());
        if (ObjectUtils.isNotEmpty(appIds)) {
            repository.appList(appIds, new ResultCallBack<Result<List<AppEntity>>>() {
                @Override
                public void onSuccess(Result<List<AppEntity>> pageResultResult) {
                    Log.e(TAG, "recommendAppList onSuccess:"+pageResultResult);
                    recommendAppList.setValue(pageResultResult.getData());
                }

                @Override
                public void onError(String errorCode, String message) {
                    Log.e(TAG, "getRecommendListData onError:"+errorCode);
                }

                @Override
                public void onComplete() {

                }
            });
        }

    }

    public void indexSearch(String search){
        setSearchData(search);
        if (isDebug){
//            searchAppList.setValue(getAppEntityList());
        }else {
            repository.indexSearch(search, new ResultCallBack<Result<CommentPage<SearchResultEntity>>>() {
                @Override
                public void onSuccess(Result<CommentPage<SearchResultEntity>> result) {
                    if (result.getResultCode().equals("200")
                            && result.getData().getPageCount() > 0
                    ) {
                        searchAppList.setValue(result.getData().getDataList());
                    }
                }

                @Override
                public void onError(String errorCode, String message) {

                }

                @Override
                public void onComplete() {

                }
            });
        }
    }

    public void setCurrentApp(AppEntity app){
        currentApp.setValue(app);
    }

    public void installApp(Context context,String appCode){
        repository.installApp(context, appCode);
    }

    //test
    private CommentPage<CommentEntity> getcurrentCommentList(){
        CommentPage<CommentEntity> commentPage = new CommentPage<>();
        List<CommentEntity> commentEntityList = new ArrayList<>();
        commentPage.setDataList(commentEntityList);
        commentPage.setPageCount(3);
        commentPage.setPageNum(0);
        commentPage.setPageSize(10);
        commentPage.setPageTime(""+System.currentTimeMillis());
        commentPage.setTotalCount(26);
        for (int i=0; i<10; i++){
            CommentEntity entity = getCommentEntity(i);
            List<ReplyEntity> replyEntityList = new ArrayList<>();
            for (int j=0; j<10; j++){
                CommentEntity commentEntity = getCommentEntity(j);
                ReplyEntity replyEntity = new ReplyEntity();
                BeanUtils.copyProperties(commentEntity, replyEntity);
                replyEntity.setToReplyUser(getPublisherEntity(j));
                replyEntity.setType(1);
                replyEntity.setCreateTime(""+System.currentTimeMillis());
                replyEntity.setPublisher(getPublisherEntity(j));
                replyEntity.setContent("评价回复、评价回复、评价回复");
                replyEntityList.add(replyEntity);
            }
            entity.setReplyList(replyEntityList);
            entity.setReplyCount(replyEntityList.size());

            commentEntityList.add(entity);
        }
        return commentPage;
    }
    //test
    private CommentEntity getCommentEntity(int i) {
        CommentEntity entity = new CommentEntity();
        entity.setId(String.valueOf(i));
        entity.setPublisher(getPublisherEntity(i));
        entity.setScore("2");
        entity.setContent("sdkjdhaksdhdafkushfufhafasdkjnda");
        entity.setUrl("url");
        entity.setPraiseCount(1000);
        entity.setCreateTime(""+System.currentTimeMillis());
        entity.setIsPraise(0);
        entity.setIsBoutique(1);
        entity.setIsAuthorReply(1);
        entity.setModelAbbreviation("R7");
        return entity;
    }
    //test
    private PublisherEntity getPublisherEntity(int i) {
        PublisherEntity publisherEntity = new PublisherEntity();
        publisherEntity.setUserId(""+i);
        publisherEntity.setIsAuthor(0);
        publisherEntity.setNickName("发布人昵称");
        publisherEntity.setHeadPortraitUrl("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F201712%2F08%2F20171208214551_AFQ2h.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1641825421&t=8a7a08d92a639b73274a88ac6dbedaa6");
        return publisherEntity;
    }
    //test
    private List<AppEntity> getAppEntityList(){
        List<AppEntity> list = new ArrayList<>();
        for (int i=0; i<10; i++){
            AppEntity entity = new AppEntity();
            entity.setCode("appcode"+i);
            entity.setName("app-name"+i);
            entity.setLabels("标签1,标签2,标签3");
            entity.setOneWord("一句话描述APP"+i);
            entity.setSort(i);
            entity.setDeveloper("开发者");
            entity.setProducer("阿里巴巴集团");
            entity.setSize("123M");
            entity.setScore(""+i+".0");
            entity.setPreviews("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic2.zhimg.com%2F50%2Fv2-b37ae51099512ecc2f15f126a592c547_hd.jpg&refer=http%3A%2F%2Fpic2.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1641825422&t=ed2f4135454d59f5be1d837b3405834b");
            entity.setIcon("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Ff1e9d1d4a9a07d8bbe462b83458d42dc3d3cd99321a7a-81sVBh_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1641564401&t=d43503eec4ebc4df82054433b94fee3a");
            entity.setComment("Anna：阿萨德看看是否拉客户封口费half哈弗垃圾爱上了对方half吉安市福利卡回复"+i);
            entity.setAdaptiveModel("R7");
            entity.setNetProtocol("4G 5G");
            entity.setOfficialGroup("23424234");
            entity.setDesc("description、description、description、description、description");
            entity.setDeveloperWord("DeveloperWord、DeveloperWord、DeveloperWord、DeveloperWord");
            entity.setType(i%2+1);
            entity.setVersion("V 1."+i);
            entity.setUrl("http://ucan.25pp.com/Wandoujia_web_seo_baidu_homepage.apk");
            entity.setUpdateTime(new Date().toString());
            list.add(entity);
        }
        return list;
    }

}